﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCFServicesForCitrix.EC2
{
    public class EC2 : Interfaces.IEC2
    {
        public List<EC2Instance> GetInstances(string AccessKey, string SecretKey)
        {
            List<EC2Instance> returnedEC2Instances = new List<EC2Instance>();

            Amazon.EC2.AmazonEC2 ec2 = Amazon.AWSClientFactory.CreateAmazonEC2Client(AccessKey,SecretKey);

            Amazon.EC2.Model.DescribeInstancesRequest request = new Amazon.EC2.Model.DescribeInstancesRequest();
            Amazon.EC2.Model.DescribeInstancesResponse response = ec2.DescribeInstances(request);

            foreach (Amazon.EC2.Model.Reservation InstanceStore in response.DescribeInstancesResult.Reservation)
            {
                foreach (Amazon.EC2.Model.RunningInstance instance in InstanceStore.RunningInstance)
                {
                    EC2Instance ec2VMInstance = new EC2Instance();
                    ec2VMInstance.Name = instance.InstanceId;
                    returnedEC2Instances.Add(ec2VMInstance);
                }
            }
            return returnedEC2Instances;
        }
        public EC2InstanceInfo GetInstanceInfo(string AccessKey, string SecretKey, EC2Instance Instance)
        {
            EC2InstanceInfo returnedInstanceInfo = new EC2InstanceInfo();

            Amazon.EC2.AmazonEC2 ec2 = Amazon.AWSClientFactory.CreateAmazonEC2Client(AccessKey, SecretKey);

            Amazon.EC2.Model.DescribeInstancesRequest request = new Amazon.EC2.Model.DescribeInstancesRequest();
            request.InstanceId = new List<string>{Instance.Name};

            Amazon.EC2.Model.DescribeInstancesResponse response = ec2.DescribeInstances(request);
           
            foreach (Amazon.EC2.Model.Reservation InstanceStore in response.DescribeInstancesResult.Reservation)
            {
                foreach (Amazon.EC2.Model.RunningInstance ec2VM in InstanceStore.RunningInstance)
                {
                    returnedInstanceInfo.InstanceType = ec2VM.InstanceType;
                    returnedInstanceInfo.LaunchTime = ec2VM.LaunchTime;
                    returnedInstanceInfo.Platform = ec2VM.Platform;
                    returnedInstanceInfo.PrivateDNSName = ec2VM.PrivateDnsName;
                    returnedInstanceInfo.PrivateIPAddress = ec2VM.PrivateIpAddress;
                    returnedInstanceInfo.PublicDNSName = ec2VM.PublicDnsName;
                    returnedInstanceInfo.PublicIPAddress = ec2VM.IpAddress;
                    returnedInstanceInfo.State = ec2VM.InstanceState.Name;
                }
            }

            
            return returnedInstanceInfo;
        }
        public EC2InstanceStateChange StartInstance(string AccessKey, string SecretKey, string InstanceID)
        {
            EC2InstanceStateChange returnedStateChange = new EC2InstanceStateChange();

            Amazon.EC2.AmazonEC2 ec2 = Amazon.AWSClientFactory.CreateAmazonEC2Client(AccessKey, SecretKey);

            Amazon.EC2.Model.StartInstancesRequest vmStartRequest = new Amazon.EC2.Model.StartInstancesRequest();
            vmStartRequest.InstanceId = new List<string> { InstanceID };

            Amazon.EC2.Model.StartInstancesResponse vmStartResponse = ec2.StartInstances(vmStartRequest);
            Amazon.EC2.Model.StartInstancesResult vmStartResult = vmStartResponse.StartInstancesResult;
            foreach ( Amazon.EC2.Model.InstanceStateChange stateChange in vmStartResult.StartingInstances )
            {
                returnedStateChange.CurrentState = stateChange.CurrentState.Name;
                returnedStateChange.PreviousState = stateChange.PreviousState.Name;
            }

            return returnedStateChange;
        }
        public EC2InstanceStateChange StopInstance(string AccessKey, string SecretKey, string InstanceID)
        {
            EC2InstanceStateChange returnedStateChange = new EC2InstanceStateChange();

            Amazon.EC2.AmazonEC2 ec2 = Amazon.AWSClientFactory.CreateAmazonEC2Client(AccessKey, SecretKey);

            Amazon.EC2.Model.StopInstancesRequest vmStopRequest = new Amazon.EC2.Model.StopInstancesRequest();
            vmStopRequest.InstanceId = new List<string> { InstanceID };

            Amazon.EC2.Model.StopInstancesResponse vmStopResponse = ec2.StopInstances(vmStopRequest);
            Amazon.EC2.Model.StopInstancesResult vmStopResult = vmStopResponse.StopInstancesResult;
            foreach (Amazon.EC2.Model.InstanceStateChange stateChange in vmStopResult.StoppingInstances)
            {
                returnedStateChange.CurrentState = stateChange.CurrentState.Name;
                returnedStateChange.PreviousState = stateChange.PreviousState.Name;
            }

            return returnedStateChange;
        }
        public bool DeleteInstance(string AccessKey, string SecretKey, string InstanceID)
        {
            return true;
        }

    }
}
